function promisify(originFunc: Function, { ctx, callbackIndex = -1, hasExtra = false }) {

  return function func (...originArgs: any[]) {
    return new Promise((resolve: Function) => {
      const args = [...originArgs]

      ctx = ctx || this
      if (callbackIndex === -1) callbackIndex = args.length
      /*
      * if callback has few params
      */
      const callback = hasExtra ? (...args) => resolve(args) : resolve

      args.splice(callbackIndex, 0, callback)
      originFunc.apply(ctx, args)
    })
  }
}

export default promisify
